#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstdlib>

#include "common.h"
#include "ReadFile.h"

using namespace std;

int convert_from_IPC()
{
    float tPower = 0;
    float pSum = 0;
    ofstream ofs;
    ofs.open ("SPAN_power.out");
    ifstream ifs ("SPAN.out", ifstream::in);
    if (ifs.good())
    {
        while (!ifs.eof())
        {
            string powerText;
            getline (ifs, powerText);
            stringstream sTestLine (powerText);
            string str;

            while(getline (sTestLine,str,','))
            {
                stringstream buf;
                buf.str(str);
                buf >> tPower;
                pSum += 2.76+tPower*3.83;
                buf.clear();
            }
            if (pSum != 0)
                ofs << pSum << "\n";
            pSum = 0;
        }
        ifs.close();
        ofs.close();
        return 0;
    }
    else
        exit(1);
}

int ReadFlie(vector<float>* vPower){
      string powerText;
      float tPower;

      ifstream ifs ("SPAN_power.out", ifstream::in);

      if (ifs.good())
      {
          while (!ifs.eof())
          {
              getline (ifs, powerText);
              stringstream sTestLine (powerText);
              sTestLine >> tPower;
              vPower->push_back(tPower);
          }
          ifs.close();
          if(vPower->size() > 0)
            return 0;
          else
            {
                cerr << "SPAN_ERROR: The power information in the"
                     << "file is not valide in function ReadFile()." << endl;
                exit(1);
            }
      }
      else
      {
          cerr << "SPAN_ERROR: Can't open the file." << endl;
          exit(1);
      }
}


void PrintTraverse (vector<float>* vPower){
    cerr << "Begin traverse\n" << endl;
    vector<float>::iterator itPower;
    if((vPower->size())>0)
    {
        itPower = vPower->begin();
        while(itPower < vPower->end())
        {
            cerr << *itPower << endl;
            itPower++;
        }
    }
    else
        cerr << "This is an empty array" << endl;
}

int MinMaxValue(float* min, float* max, vector<float>* vPower){
    if(vPower -> size() > 0)
    {
        vector<float>::iterator itPower;
        itPower = vPower->begin();
        *max = *itPower;
        *min = *itPower;
        while(itPower < vPower->end())
        {
            if(*itPower < *min)
                *min = *itPower;
            if(*itPower > *max)
                *max = *itPower;
            itPower++;
        }
        if((*max - *min) > NCOLOR )
        {
        cerr << "SPAN_WARNING: The difference of min and max power"
             << "values is greater than 255W. " << endl;
        }
        return 0;
    }
    else
    {
        cerr << "SPAN_ERROR: There power information in the file is"
             << "not valide in function MinMaxValue()." << endl;
        exit(1);
    }
}

int ReadFlieProgram(vector<string>* pName, vector<int>* pCount){
      string pText;
      int iCount;
      int count = 0;

      ifstream ifs ("SPAN_program.out", ifstream::in);

      if (ifs.good())
      {
          while (!ifs.eof())
          {
              getline (ifs, pText);
              if (pText != ""){
                  if (count % 2 == 0)
                  {
                      pName->push_back(pText);
                  }
                  else
                  {
                      stringstream sTestLine (pText);
                      sTestLine >> iCount;
                      pCount->push_back(iCount);
                  }
                  count++;
                  //printf("%d\n",count);
              }
          }
          ifs.close();
          if((pName->size() > 0) && pName->size() == pCount->size())
              return 0;
          else
            {
                cerr << "There is no power information in the file"
                     << " or the information is not complete in"
                     << "function ReadFileProgram()" << endl;
                exit(1);
            }
      }
      else{
          cerr << "SPAN_ERROR: Can't open the file." << endl;
          exit(1);
          }
}

